package uk.co.aetherworks.osc_messaging;

/*
 * Ped-ome
 *
 * PedToApp - Sends Messages to Monome Application
 *
 * Joe Handford aka Knecht 2011
 *
 */ 

// OSC Include Files
import com.illposed.osc.OSCPortOut;
import com.illposed.osc.OSCPortIn;
import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;

// Java Net Include Files

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

// aetherworks imports
import uk.co.aetherworks.debug.*;

public class PedToApp extends Debuggable {

	OSCPortOut sender;
	String appPrefix = "/pedome";
	
	// appListeningPort picked up from /sys/info message received from app
	public PedToApp(int appListeningPort) {
		DEBUG("** Creating PedToApp bound to port: " + appListeningPort);
		try
        {
            sender = new OSCPortOut(InetAddress.getByName("localhost"), appListeningPort);
        }
        catch (Exception e) { System.out.println("Couldn't create pedome to app sender!"); }
	}
	
	// Forward message received from monome
	public void fwdMsg(OSCMessage msg){
		DEBUG("PedToApp forwarding message from Monome");
		String address = msg.getAddress();
		Object myArgs[] = msg.getArguments();
		int x = ((Integer) myArgs[0]).intValue();
		int y = ((Integer) myArgs[1]).intValue();
		int state = ((Integer) myArgs[2]).intValue();
		DEBUG("sending " + address + " " + x + " " + y + " " + state);

		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't forward message to app!");
		}
	}
	
	// send pseudo monome messages in response to Pedome clicks
	public void sendMsg(int x, int y, int state){
        Object myArgs[] = new Object[3];
		myArgs[0] = new Integer(x);
		myArgs[1] = new Integer(y);
		myArgs[2] = new Integer(state);

		OSCMessage msg = new OSCMessage(appPrefix + "/grid/key", myArgs); // message sent for needs to change - see serialosc protocol

		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pseudo monome message to app!");
		}
	}
	
	// Send pedome transmission port
	public void sendPedomeTxPort(int pedSendingPort){
		DEBUG("    sending /sys/port " + pedSendingPort);
        Object myArgs[] = new Object[1];
		myArgs[0] = new Integer(pedSendingPort);

		OSCMessage msg = new OSCMessage("/sys/port", myArgs);

		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pedome tx port to app as connection confirmation");
		}
	}

	// Confirm app prefix set by pedome
	public void sendAppPrefix(String prefix) {
		DEBUG("    sending /sys/prefix " + prefix);
        Object myArgs[] = new Object[1];
		myArgs[0] = prefix;

		OSCMessage msg = new OSCMessage("/sys/prefix", myArgs);

		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pedome tx port to app as connection confirmation");
		}
	}

	// Send pedome info
	public void sendPedomeInfo(int pedSendingPort){
		DEBUG("    sending /sys/port " + pedSendingPort);
		DEBUG("    sending /sys/prefix " + appPrefix);
		DEBUG("    sending /sys/id pedome ");
        Object myArgs[] = new Object[1];
		myArgs[0] = new Integer(pedSendingPort);

		OSCMessage msg = new OSCMessage("/sys/port", myArgs);

		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pedome tx port to app as connection confirmation");
		}
		
		myArgs[0] = appPrefix;
		msg = new OSCMessage("/sys/prefix", myArgs);
		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pedome prefix to app as connection confirmation");
		}
		

		myArgs[0] = new String("pedome");
		msg = new OSCMessage("/sys/id", myArgs);
		try
		{
			sender.send(msg);
		}
		catch (Exception e)
		{
		 System.out.println("Couldn't send pedome prefix to app as connection confirmation");
		}
	}

	public void setAppPrefix(String prefix) {
		appPrefix = prefix;
	}
	
	public void close() {
		sender.close();
	}
}
